package cn.edu.wfit.auth.controller;

import cn.edu.wfit.api.domain.LoginUser;
import cn.edu.wfit.auth.form.LoginBody;
import cn.edu.wfit.auth.form.RegisterBody;
import cn.edu.wfit.auth.service.SysLoginService;
import cn.edu.wfit.common.core.domain.R;
import cn.edu.wfit.common.core.utils.JwtUtils;
import cn.edu.wfit.common.core.utils.StringUtils;
import cn.edu.wfit.common.security.auth.AuthUtil;
import cn.edu.wfit.common.security.service.TokenService;
import cn.edu.wfit.common.security.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.bouncycastle.asn1.x509.TargetInformation;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping()
public class LoginController {

    private final TokenService tokenService;


    private final SysLoginService sysLoginService;
    @PostMapping("login")
    public R<?> login(@RequestBody LoginBody form){
        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        // 获取登录token
        Map<String, Object> token = tokenService.createToken(userInfo);
        System.out.println("token"+token);
        return R.ok(token);
    }
    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request)
    {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            String nickName = JwtUtils.getNickName(token);

            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username, nickName);
        }
        return R.ok();
    }

    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }

    /**
     * 教师注册
     * @param registerBody
     * @return
     */
    @PostMapping("teacherRegister")
    public R<?> teacherRegister(@RequestBody RegisterBody registerBody)
    {
        registerBody.setNickname(registerBody.getUsername());

        //102是教师权限

        // 用户注册
        sysLoginService.register(registerBody.getUsername(), registerBody.getNickname(), registerBody.getPassword());
        return R.ok();
    }

    @PostMapping("register")
    public R<?> register(@RequestBody RegisterBody registerBody)
    {
        registerBody.setNickname(registerBody.getUsername());
        // 用户注册
        sysLoginService.register(registerBody.getUsername(), registerBody.getNickname(), registerBody.getPassword());
        return R.ok();
    }
    @PostMapping("test")
    public String test(HttpServletRequest request) throws NoSuchAlgorithmException {
     /*  String token = "eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6IjE5NGQ4OTZiLTk2OTktNGNkOC04NGZiLWY1MmMzZjRlZTg4MSIsInVzZXJuYW1lIjoiYWRtaW4ifQ.1eGZkGB54o0HNKTGtVb1dTXKedKID2hrZiipd-YcKL0";
        LoginUser loginUser = tokenService.getLoginUser(token);
        System.out.println(loginUser);*/
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            String nickName = JwtUtils.getNickName(token);
            System.out.println(username);
            System.out.println(nickName);
        }
        return "888";
    }
}
